package com.doeis.minaserver.packet;

import com.doeis.common.utils.DateUtils;
import com.doeis.minaserver.bean.ServerMessage;
import com.doeis.minaserver.packet.Packet.Direction;
import com.doeis.minaserver.packet.Packet.PacketType;
import com.doeis.minaserver.packet.Packet.ProtocolVersion;
import com.doeis.uwork.packet.GroupMsgPacket;
import com.doeis.uwork.packet.MessagePacket;
import com.doeis.uwork.packet.PrivateMsgPacket;
import com.voland.core.utils.IDGenerator;

/**
 * 服务器数据包的工厂类
 * @author nortan
 *
 */
public class ServerPacketFactory extends PacketFactory {
	
	public static InfoPacket systemMsg(String to, String msg, int type,byte cmd){
		InfoPacket packet = createInfoPacket(to);
		packet.setCmd( cmd );
		packet.setNotifyType( type );
		ServerMessage sm = new ServerMessage();
		sm.setServer_message(msg);		
		packet.setBodyObj(sm);
		return packet;		
	}
	
	/**
	 * 生成服务器端的信息包
	 * @return
	 */
	public static InfoPacket createInfoPacket(String to){
		InfoPacket packet = new InfoPacket();
		packet.setDirection(Direction.s2c);
		packet.setPacketType(PacketType.info);
		packet.setVersion(ProtocolVersion.V1_0_0);
		packet.setFrom( Packet.SERVER_ID );
		packet.setTo(to);
		packet.setTime(DateUtils.currentTime());
		return packet;
	}
	
	public static InfoPacket createInfoPacket(String from,String to){
		InfoPacket packet = new InfoPacket();
		packet.setDirection(Direction.c2s);
		packet.setPacketType(PacketType.info);
		packet.setVersion(ProtocolVersion.V1_0_0);		
		packet.setTo( to );
		packet.setFrom( from );
		return packet;
	}
	
	
	
	/**
	 * 回复用户登陆的信息包
	 * @param notifyStatus  用户登陆情况
	 * @return
	 */
	public static InfoPacket replyUserLogin(InfoPacket p){
		InfoPacket info = createInfoPacket( p.getFrom() );
		info.setCmd(InfoPacket.NOTIFY_USER_LOGIN);
		info.setBody(p.getBody());
		return info;
	}
	
	public static InfoPacket replyPushoK(InfoPacket p){
		InfoPacket info = createInfoPacket( p.getFrom() );
		info.setCmd(InfoPacket.NOTIFY_BAI_PUSH);
		info.setBody(p.getBody());
		return info;
	}
	
	/**
	 * 回复用户被踢的信息包
	 * @param notifyStatus  用户登陆情况
	 * @return
	 */
	public static InfoPacket replyUserLoginOut(InfoPacket p){
		InfoPacket info = createInfoPacket( p.getFrom() );
		info.setCmd(InfoPacket.NOTIFY_USER_LOGIN_OUT);
		info.setBody(p.getBody());
		return info;
	}
	
	/**
	 * 服务器发送的心跳包
	 * @param notifyStatus  用户登陆情况
	 * @return
	 */
	public static InfoPacket sendHeart(String to){
		InfoPacket info = createInfoPacket( to );
		info.setCmd(InfoPacket.NOTIFY_HEART_BEAT);
		info.setBody("服务器-》 "+ to +" 发送心跳包");
		return info;
	}
	
	/**
	 * 生成服务器端的消息包
	 * @return
	 */
	public static MessagePacket createMessagePacket(String to){
		MessagePacket packet = new MessagePacket();
		packet.setDirection(Direction.s2c);
		packet.setPacketType(PacketType.message);
		packet.setVersion(ProtocolVersion.V1_0_0);
		packet.setFrom( Packet.SERVER_ID );
		packet.setTo(to);
		packet.setTime(DateUtils.currentTime());
		return packet;
	}
	
	
	/**
	 * 回复聊天的消息包
	 * @return
	 */
	public static MessagePacket replyMessage(MessagePacket p){
		MessagePacket info = createMessagePacket( p.getTo() );
		info.setCmd(MessagePacket.NOTIFY_MSG_REPLY);
		info.setBody(p.getBody());
		return info;
	}
	/**
	 * 回复聊天的消息包
	 * @return
	 */
	public static MessagePacket replyOutMessage(MessagePacket p){
		MessagePacket info = createMessagePacket( p.getTo() );
		info.setCmd(MessagePacket.NOTIFY_MSG_REPLY_OUT);
		info.setBody(p.getBody());
		return info;
	}
	
	/**
	 * 回复聊天增加的消息包
	 * @return
	 */
	public static MessagePacket replyAddMessage(MessagePacket p){
		MessagePacket info = createMessagePacket( p.getTo() );
		info.setCmd(MessagePacket.NOTIFY_MSG_ADD);
		info.setBody(p.getBody());
		return info;
	}
	/**
	 * 回复聊天增加的消息包
	 * @return
	 */
	public static MessagePacket replyAddOtherMessage(MessagePacket p){
		MessagePacket info = createMessagePacket( p.getTo() );
		info.setCmd(MessagePacket.NOTIFY_MSG_ADD_OTHER);
		info.setBody(p.getBody());
		return info;
	}
	
	/**
	 * 回复聊天列表的消息包
	 * @return
	 */
	public static MessagePacket replyListMessage(MessagePacket p){
		MessagePacket info = createMessagePacket( p.getTo() );
		info.setCmd(MessagePacket.NOTIFY_MSG_LIST);
		info.setBody(p.getBody());
		return info;
	}

	/**
	 * 添加组外聊天的消息包
	 * @return
	 */
	public static GroupMsgPacket replyAddGroupMessage(String to, String body) {
		GroupMsgPacket info = createGroupMessagePacket( to );
		info.setCmd(GroupMsgPacket.NOTIFY_MSG_ADD);
		info.setBody(body);
		return info;
	}
	/**
	 * 添加组内聊天的消息包
	 * @return
	 */
	public static PrivateMsgPacket replyAddPrivateMessage(PrivateMsgPacket packet) {
		PrivateMsgPacket info = createPrivateMessagePacket( packet.getTo() );
		info.setCmd(PrivateMsgPacket.NOTIFY_MSG_ADD);
		info.setBody(packet.getBody());
		return info;
	}

	public static PrivateMsgPacket createPrivateMessagePacket(String to) {
		PrivateMsgPacket packet = new PrivateMsgPacket();
		packet.setDirection(Direction.s2c);
		packet.setPacketType(PacketType.privateMsg);
		packet.setVersion(ProtocolVersion.V1_0_0);
		packet.setFrom( Packet.SERVER_ID );
		packet.setTo(to);
		packet.setTime(DateUtils.currentTime());
		return packet;
	}

	public static GroupMsgPacket createGroupMessagePacket(String to) {
		GroupMsgPacket packet = new GroupMsgPacket();
		packet.setDirection(Direction.s2c);
		packet.setPacketType(PacketType.groupMsg);
		packet.setVersion(ProtocolVersion.V1_0_0);
		packet.setFrom( Packet.SERVER_ID );
		packet.setTo(to);
		packet.setTime(DateUtils.currentTime());
		return packet;
	}
	
	
	
	
}
